home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / db_xl_vb.zip / DB-XL.FRM next >
Text File  |  1994-04-07  |  14KB  |  484 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   5460
  7.    ClientLeft      =   750
  8.    ClientTop       =   1530
  9.    ClientWidth     =   6735
  10.    Height          =   5865
  11.    Left            =   690
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   5460
  14.    ScaleWidth      =   6735
  15.    Top             =   1185
  16.    Width           =   6855
  17.    Begin FileListBox File2 
  18.       Height          =   1200
  19.       Left            =   4080
  20.       TabIndex        =   24
  21.       Top             =   720
  22.       Visible         =   0   'False
  23.       Width           =   975
  24.    End
  25.    Begin PictureBox Picture2 
  26.       Align           =   2  'Align Bottom
  27.       BackColor       =   &H00C0C0C0&
  28.       Height          =   255
  29.       Left            =   0
  30.       ScaleHeight     =   225
  31.       ScaleWidth      =   6705
  32.       TabIndex        =   22
  33.       Top             =   5205
  34.       Width           =   6735
  35.       Begin Label Label7 
  36.          AutoSize        =   -1  'True
  37.          BackStyle       =   0  'Transparent
  38.          Caption         =   "Label7"
  39.          Height          =   192
  40.          Left            =   120
  41.          TabIndex        =   23
  42.          Top             =   0
  43.          Width           =   576
  44.       End
  45.    End
  46.    Begin PictureBox Picture1 
  47.       AutoSize        =   -1  'True
  48.       Height          =   1635
  49.       Left            =   4320
  50.       Picture         =   DB-XL.FRX:0000
  51.       ScaleHeight     =   1605
  52.       ScaleWidth      =   2370
  53.       TabIndex        =   21
  54.       Top             =   240
  55.       Width           =   2400
  56.    End
  57.    Begin TextBox Text1 
  58.       Height          =   372
  59.       Left            =   1920
  60.       TabIndex        =   20
  61.       Text            =   "Text1"
  62.       Top             =   1200
  63.       Width           =   2052
  64.    End
  65.    Begin CommandButton Command3 
  66.       Cancel          =   -1  'True
  67.       Caption         =   "Exit"
  68.       Height          =   492
  69.       Left            =   4320
  70.       TabIndex        =   18
  71.       Top             =   4320
  72.       Width           =   1932
  73.    End
  74.    Begin FileListBox File1 
  75.       Height          =   1200
  76.       Left            =   4320
  77.       TabIndex        =   9
  78.       Top             =   2280
  79.       Width           =   1935
  80.    End
  81.    Begin DirListBox Dir1 
  82.       Height          =   1752
  83.       Left            =   2040
  84.       TabIndex        =   8
  85.       Top             =   2280
  86.       Width           =   1932
  87.    End
  88.    Begin DriveListBox Drive1 
  89.       Height          =   288
  90.       Left            =   4320
  91.       TabIndex        =   12
  92.       Top             =   3720
  93.       Width           =   1932
  94.    End
  95.    Begin Frame Frame1 
  96.       BackColor       =   &H00C0C0C0&
  97.       Caption         =   "Database"
  98.       Height          =   2892
  99.       Left            =   240
  100.       TabIndex        =   0
  101.       Top             =   1920
  102.       Width           =   1452
  103.       Begin OptionButton Option1 
  104.          BackColor       =   &H00C0C0C0&
  105.          Caption         =   "Paradox 3.x"
  106.          Height          =   252
  107.          Index           =   6
  108.          Left            =   120
  109.          TabIndex        =   7
  110.          Top             =   2520
  111.          Width           =   1212
  112.       End
  113.       Begin OptionButton Option1 
  114.          BackColor       =   &H00C0C0C0&
  115.          Caption         =   "Btrieve"
  116.          Height          =   252
  117.          Index           =   5
  118.          Left            =   120
  119.          TabIndex        =   6
  120.          Top             =   2160
  121.          Width           =   1212
  122.       End
  123.       Begin OptionButton Option1 
  124.          BackColor       =   &H00C0C0C0&
  125.          Caption         =   "FoxPro 2.5"
  126.          Height          =   252
  127.          Index           =   4
  128.          Left            =   120
  129.          TabIndex        =   5
  130.          Top             =   1800
  131.          Width           =   1212
  132.       End
  133.       Begin OptionButton Option1 
  134.          BackColor       =   &H00C0C0C0&
  135.          Caption         =   "FoxPro 2.0"
  136.          Height          =   252
  137.          Index           =   3
  138.          Left            =   120
  139.          TabIndex        =   4
  140.          Top             =   1440
  141.          Width           =   1212
  142.       End
  143.       Begin OptionButton Option1 
  144.          BackColor       =   &H00C0C0C0&
  145.          Caption         =   "dBase IV"
  146.          Height          =   252
  147.          Index           =   2
  148.          Left            =   120
  149.          TabIndex        =   3
  150.          Top             =   1080
  151.          Width           =   1212
  152.       End
  153.       Begin OptionButton Option1 
  154.          BackColor       =   &H00C0C0C0&
  155.          Caption         =   "dBase III"
  156.          Height          =   252
  157.          Index           =   1
  158.          Left            =   120
  159.          TabIndex        =   2
  160.          Top             =   720
  161.          Width           =   1212
  162.       End
  163.       Begin OptionButton Option1 
  164.          BackColor       =   &H00C0C0C0&
  165.          Caption         =   "Access 1.x"
  166.          Height          =   252
  167.          Index           =   0
  168.          Left            =   120
  169.          TabIndex        =   1
  170.          Top             =   360
  171.          Width           =   1212
  172.       End
  173.    End
  174.    Begin ComboBox Combo1 
  175.       Height          =   288
  176.       Left            =   1920
  177.       Style           =   2  'Dropdown List
  178.       TabIndex        =   10
  179.       Top             =   240
  180.       Width           =   2052
  181.    End
  182.    Begin CommandButton Command1 
  183.       Caption         =   "Convert"
  184.       Default         =   -1  'True
  185.       Height          =   492
  186.       Left            =   2040
  187.       TabIndex        =   11
  188.       Top             =   4320
  189.       Width           =   1932
  190.    End
  191.    Begin Label Label6 
  192.       BackStyle       =   0  'Transparent
  193.       Caption         =   "To Spreadsheet:"
  194.       Height          =   252
  195.       Left            =   360
  196.       TabIndex        =   19
  197.       Top             =   1200
  198.       Width           =   1452
  199.    End
  200.    Begin Label Label5 
  201.       BackStyle       =   0  'Transparent
  202.       Caption         =   "Label5"
  203.       Height          =   252
  204.       Left            =   1920
  205.       TabIndex        =   17
  206.       Top             =   720
  207.       Width           =   2292
  208.    End
  209.    Begin Label Label4 
  210.       BackStyle       =   0  'Transparent
  211.       Caption         =   "From Database:"
  212.       Height          =   252
  213.       Left            =   360
  214.       TabIndex        =   16
  215.       Top             =   720
  216.       Width           =   1452
  217.    End
  218.    Begin Label Label3 
  219.       BackStyle       =   0  'Transparent
  220.       Caption         =   "Convert Table:"
  221.       Height          =   252
  222.       Left            =   480
  223.       TabIndex        =   15
  224.       Top             =   240
  225.       Width           =   1332
  226.    End
  227.    Begin Label Label2 
  228.       BackStyle       =   0  'Transparent
  229.       Caption         =   "Database or Table:"
  230.       Height          =   252
  231.       Left            =   4320
  232.       TabIndex        =   14
  233.       Top             =   1920
  234.       Width           =   1932
  235.    End
  236.    Begin Label Label1 
  237.       BackStyle       =   0  'Transparent
  238.       Caption         =   "Path of Database:"
  239.       Height          =   252
  240.       Left            =   2040
  241.       TabIndex        =   13
  242.       Top             =   1920
  243.       Width           =   1932
  244.    End
  245. End
  246. 'This sample program shows you how to combine programming
  247. 'with database objects and ole automation objects.
  248. 'This program will convert a table in a database that
  249. 'the user selects, and then places it into an excel
  250. 'spreadsheet using OLE automation.
  251.  
  252. 'This program assumes that you have registered Excel version
  253. '5.0 in your registration database (REG.DAT) and that
  254. 'you installed the database component for Visual Basic 3.0
  255. 'Professional.
  256.  
  257. Dim db As database     'form level database object
  258. Dim Connect$           'Hold connect arguments
  259.  
  260. Sub CheckEnableConvert ()
  261.  
  262. 'check if table is selected and filename specified
  263. If (combo1.Text <> "") And (Text1 <> "") Then
  264.     command1.Enabled = True
  265. Else
  266.     command1.Enabled = False
  267. End If
  268.  
  269. End Sub
  270.  
  271. Sub Combo1_Click ()
  272.  
  273. Call CheckEnableConvert
  274.  
  275. End Sub
  276.  
  277. Sub Command1_Click ()
  278. Static flag As Integer  'flag for avoiding multiple occurances
  279. Dim i As Integer        'loop counters
  280. Dim j As Integer
  281. Dim xl As object        'ole automation object
  282. Dim Sn As Snapshot      'snapshot to hold records
  283.  
  284. If flag = 1 Then Exit Sub  'avoid multiple clicks
  285. flag = 1
  286.  
  287. screen.MousePointer = 11   'change mousepointer
  288.  
  289. 'This code performs a check for valid path and filenames
  290. 'The hidden File2 listbox has the sole purpose of validating that the
  291. 'user has entered a valid path and filename
  292. CheckPath:
  293.   label7.Caption = "Checking Valid Filename for Spreadsheet"
  294.   label7.Refresh
  295.   Text1.Tag = True            'flag if invalid filename in textbox
  296.   Do While Text1.Tag
  297.     On Error Resume Next
  298.     File2.FileName = Text1.Text
  299.     File2.Refresh
  300.     If Err = 0 Then                 'no errors
  301.         If InStr(Text1.Text, File2.List(0)) > 0 Then 'kill file if it exists
  302.             Kill Text1.Text
  303.             Text1.Tag = False
  304.         Else                        'just a directory entry, get filename
  305.             Text1.Text = InputBox("You Entered an Invalid Path or Filename, please enter a correct one: ", "DB to Excel Converter", Text1)
  306.         End If
  307.     Else
  308.         If Err <> 53 Then           'if not "file not found", get valid path/filename
  309.             Text1.Text = InputBox("You Entered an Invalid Path or Filename, please enter a correct one: ", "DB to Excel Converter", Text1)
  310.         Else
  311.             Text1.Tag = False       'valid new filename
  312.         End If
  313.     End If
  314.     On Error GoTo 0
  315.   Loop
  316.  
  317. 'create our spreadsheet object
  318. label7.Caption = "Creating Excel Object"
  319. label7.Refresh
  320. Set xl = CreateObject("Excel.Sheet.5")
  321.  
  322. 'set up Field names as Column names
  323. Set Sn = db.CreateSnapshot(combo1.Text)
  324. If Sn.RecordCount > 0 Then
  325.     Sn.MoveFirst
  326.  
  327.     'place the fields across the top of the spreadsheet
  328.     label7.Caption = "Adding fieldnames to Spreadsheet"
  329.     label7.Refresh
  330.     For i = 0 To Sn.Fields.Count - 1
  331.         xl.cells(1, i + 1).value = Sn(i).Name
  332.     Next
  333.     
  334.     'get an accurate recordcount before we start our loop
  335.     Sn.MoveLast
  336.     Sn.MoveFirst
  337.  
  338.     'loop through each record
  339.     For i = 0 To Sn.RecordCount - 1
  340.         label7.Caption = "Looping through record " & CStr(i + 1) & " of " & CStr(Sn.RecordCount)
  341.         label7.Refresh
  342.         For j = 0 To Sn.Fields.Count - 1
  343.             'add each field to the spreadsheet
  344.             If Sn(j).Type < 11 Then
  345.                 xl.cells(i + 2, j + 1).value = Sn(j)
  346.             Else
  347.                 xl.cells(i + 2, j + 1).value = "binary data"
  348.             End If
  349.         Next j
  350.         Print
  351.         Sn.MoveNext
  352.     Next i
  353.     
  354.     'save the spreadsheet
  355.     label7.Caption = "Saving Spreadsheet"
  356.     label7.Refresh
  357.     xl.SaveAs Text1.Text
  358.  
  359.     'quit the excel object
  360.     xl.Application.Quit
  361.  
  362. Else
  363.     'no records in recordset
  364.     label7.Caption = "No Records"
  365.     label7.Refresh
  366.     
  367.     'Pause for fraction of a second to display message
  368.     x = Timer
  369.     While x + .3 > Timer
  370.     Wend
  371. End If
  372.  
  373. 'clean up
  374. label7.Caption = "Cleaning Up"
  375. label7.Refresh
  376. Set xl = Nothing         'remove object variable
  377. Set Sn = Nothing         'remove snapshot object
  378. screen.MousePointer = 0  'restore mouse pointer
  379. flag = 0                 'allow user to click again
  380.  
  381. label7.Caption = "Ready"
  382. label7.Refresh
  383.  
  384. End Sub
  385.  
  386. Sub Command3_Click ()
  387. End   'end the program
  388.  
  389. End Sub
  390.  
  391. Sub Dir1_Change ()
  392. File1.Path = Dir1.Path
  393. End Sub
  394.  
  395. Sub Drive1_Change ()
  396. Dir1.Path = Drive1.Drive
  397.  
  398. End Sub
  399.  
  400. Sub File1_Click ()
  401. 'This subroutine loads the Table combo box from the selected database
  402. Const DB_SYSTEMOBJECT = &H80000002  'constant to check for system variables
  403. Dim i As Integer
  404. Dim DBName$
  405.  
  406. 'set up database object
  407. If (Connect$ = "") Or (Connect$ = "Btrieve") Then
  408.     DBName$ = File1.Path & "\" & File1.FileName
  409. Else
  410.     DBName$ = File1.Path
  411. '    Set db = OpenDatabase(File1.Path, False, False, Connect$)
  412. '    label5.Caption = File1.Path
  413. End If
  414.  
  415. Set db = OpenDatabase(DBName$, False, False, Connect$)
  416. label5.Caption = DBName$
  417. label5.Refresh
  418.  
  419. 'clear the tables combo box
  420. combo1.Clear
  421.  
  422. 'add new tables except system tables to the combo box
  423. For i = 0 To db.TableDefs.Count - 1
  424.     If (db.TableDefs(i).Attributes And DB_SYSTEMOBJECT) = 0 Then
  425.         combo1.AddItem db.TableDefs(i)
  426.     End If
  427. Next i
  428.  
  429. 'set the combo box to point to the first table in the list
  430. combo1.ListIndex = 0
  431.  
  432. End Sub
  433.  
  434. Sub File1_PathChange ()
  435. 'if no items in file list box, clear the combobox
  436. If File1.ListCount = 0 Then
  437.     combo1.ListIndex = -1
  438.     combo1.Clear
  439. End If
  440.  
  441. End Sub
  442.  
  443. Sub Form_Load ()
  444.  
  445. 'initialize some properties
  446. form1.Caption = "DB to Excel Converter"
  447. combo1.ListIndex = -1
  448. command1.Enabled = False
  449. Text1.Text = CurDir & "\tmp.xls"   'init the text box
  450. label5.Caption = ""                   'clear the caption
  451. label7.Caption = "Ready"              'init status bar
  452.  
  453. End Sub
  454.  
  455. Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
  456. Set db = Nothing    'destroy our global database
  457.  
  458. End Sub
  459.  
  460. Sub Option1_Click (index As Integer)
  461.  
  462. 'setup the connect property for Opendatabase
  463. Connect$ = Option1(index).Caption
  464. 'set the pattern to look for in the filelist box
  465. Select Case index
  466.     Case 0
  467.         File1.Pattern = "*.mdb"
  468.         Connect$ = ""
  469.     Case 1, 2, 3, 4
  470.         File1.Pattern = "*.dbf"
  471.     Case 5
  472.         File1.Pattern = "field.ddf"
  473.     Case 6
  474.         File1.Pattern = "*.db"
  475. End Select
  476.  
  477.  
  478. End Sub
  479.  
  480. Sub Text1_Change ()
  481. Call CheckEnableConvert
  482. End Sub
  483.  
  484.